#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: client.py
# Description: 
# 
# Edit History: 
#   2011-10-21    File created.
#===============================================================================
# Simple network client  

import socket;  
import sys;  

port = 31337;    # The port to listen on is hardcoded , if you bare changing this change the same of the server  
SIZE = 1000;  

try :  
    host = sys.argv[1];  
except :     # Add localhost as the server if no host provided  
    host = "127.0.0.1";  

try :  
    sockfd = socket.socket(socket.AF_INET , socket.SOCK_STREAM);  

except socket.error , e :  
      
    print "Error while Creating socket : ",e ;      
    sys.exit(1);  

try :  
    sockfd.connect((host,port));  

except socket.gaierror , e :  

    print "Error (Address-Related) while Connecting to server : ",e ;  

except socket.error , e :      

    print "Error while Connecting to Server : ",e;  
    sys.exit(1);  

# We are connected now , Start the real shit!  

print("============================================================="); 
print("\tSimple-CMD\tC0d3d by : 1i0n4n33sh");  
print("============================================================="); 
print("\t\tEmail : lionaneesh@gmail.com");  
print("============================================================="); 
print("\tC0d3 f0r InDi4 , H4(k F0r 1nDi4 , Liv3 f0r 1nDi4");  
print("============================================================="); 
try :  
    while 1:  
        cmd = raw_input("\n\n(simple-cmd) $ ");  
        sockfd.send(cmd);  
        result = sockfd.recv(SIZE).strip();  
        if not len(result) :  
            fd.close();  
            sockfd.close();  
            break;  
        print(result);  
except KeyboardInterrupt : #clean up code  
    sockfd.shutdown(0);  
    print("\n\n-------- Client Terminated ----------\n");  
    print("\n=================================================="); 
    print("\tThanks for using Simple-CMD");  
    print("\tEmail : lionaneesh@gmail.com");  
    print("====================================================\n\n");
